<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addPartInfo, ModifyPartInfo } from '@/api/part/partInfo';

export default {
  name: 'PartModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改', show: '明细' };
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type !== 'add') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.partNo;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择品牌', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '配件类型',
          fieldName: 'partType',
          placeholder: '请选择',
          itemList: this.createDictList('5000'),
          rules: [{ required: false, message: '请选择配件类型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '是否用品',
          fieldName: 'isThings',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否用品', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入配件代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入配件名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '拼音代码',
          fieldName: 'spellCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入拼音代码', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '配件类别',
          fieldName: 'partGroupCode',
          placeholder: '请选择',
          itemList: this.createDictList('8075'),
          rules: [{ required: false, message: '请选择配件类别', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '九大类',
          fieldName: 'partMainType',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择九大类', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '单位',
          fieldName: 'unitCode',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择单位', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '最小包装数',
          fieldName: 'minPackage',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入最小包装数', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '配件所属车型',
          fieldName: 'pateVehicleModel',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择配件所属车型', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '配件车型组集',
          fieldName: 'partModelGroupCodeSet',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择配件车型组集', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '单车用量',
          fieldName: 'quantityPerCar',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入单车用量', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '供应商名称',
          fieldName: 'providerName',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入供应商名称', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '用品品牌',
          fieldName: 'goodsBrands',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入用品品牌', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '索赔价',
          fieldName: 'claimPrice',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入索赔价', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '终端建议零售价格',
          fieldName: 'insurancePrice',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入终端建议零售价格', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '最大库存',
          fieldName: 'maxStock',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入最大库存', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '安全库存',
          fieldName: 'safeStock',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入安全库存', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '最小库存',
          fieldName: 'minStock',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入最小库存', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '常规订单价格',
          fieldName: 'regularPrice',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入常规订单价格', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '原厂编号',
          fieldName: 'oemNo',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入原厂编号', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '建议销售价',
          fieldName: 'instructPrice',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入建议销售价', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '用品大分类',
          fieldName: 'bigCategoryCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入用品大分类', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '用品中分类',
          fieldName: 'subCategoryCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入用品中分类', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '用品小分类',
          fieldName: 'thdCategoryCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入用品小分类', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否寄售',
          fieldName: 'isStorageSale',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          rules: [{ required: false, message: '请选择是否寄售', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '不含税建议销售价',
          fieldName: 'nottaxinstructprice',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入不含税建议销售价', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '替代配件',
          fieldName: 'optionNo',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入替代配件', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '替代关系',
          fieldName: 'optionRelation',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入替代关系', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '产地',
          fieldName: 'oriProCode',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入产地', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入...',
          rules: [{ required: false, message: '请输入备注', trigger: 'blur' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addPartInfo(this.saveData);
        }
        if (this.type === 'edit') {
          res = await ModifyPartInfo(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.test();
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
      this.closeDialog('refresh');
    }
  }
};
</script>

<style lang="less" scoped></style>
